package com.infotel.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.infotel.listener.NouveauListener;
import com.infotel.listener.SupprimerListener;
import com.infotel.media.DVD;

public class FenetreDVD {
    static JFrame fenetre = new JFrame();

    static JMenuBar barreMenu = new JMenuBar();
    static JMenu menuFichier = new JMenu("Fichier");
    static JMenuItem itemNouveau = new JMenuItem("Nouveau DVD");
    static JMenuItem itemQuitter = new JMenuItem("Quitter");
    String[] colonnes = { "ID", "Titre", "Auteur", "Prix", "Zone",
            "Date de sortie" };
    DefaultTableModel model = new DefaultTableModel(colonnes, 0);
    JTable tableDVD = new JTable(model);
    static JPanel panneauPrincipal = new JPanel();
    JPanel panneauSupprimer=new JPanel();
    static FenetreDVD fenetreDVD=null;
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = null;
            conn = DriverManager.getConnection("jdbc:mysql:///test");
            fenetreDVD = new FenetreDVD();
            

            panneauPrincipal.setLayout(new BorderLayout(2, 1));

            itemNouveau.addActionListener(new NouveauListener(fenetre, conn,
                    fenetreDVD));

            itemQuitter.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    System.exit(0);
                }
            });
            menuFichier.add(itemNouveau);
            menuFichier.add(itemQuitter);
            barreMenu.add(menuFichier);

            fenetre.setJMenuBar(barreMenu);
            
            JScrollPane scrollPane = new JScrollPane(panneauPrincipal);
            fenetre.add(scrollPane);
            fenetre.pack();
            fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            fenetre.setLocation(dim.width / 2 - fenetre.getWidth() / 2,
                    dim.height / 2 - fenetre.getHeight() / 2);
            fenetre.setVisible(true);
            fenetreDVD.afficherTableauDVD(conn);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void afficherTableauDVD(Connection conn) {

        ArrayList<DVD> lectureDVD = DVD.lireDVD(conn);
        panneauSupprimer.removeAll();
    
        panneauSupprimer.setLayout(new BoxLayout(panneauSupprimer, BoxLayout.Y_AXIS));
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        model.addRow(colonnes);
        panneauSupprimer.add(new JLabel("Supprimer"));
        for (int i=0;i<lectureDVD.size();i++) {
            model.addRow(lectureDVD.get(i).toArray());
            JButton button = new JButton("S");
            button.addActionListener(new SupprimerListener(Integer.parseInt(lectureDVD.get(i).toArray()[0]), i,conn,fenetreDVD));
            
            button.setSize(5, tableDVD.getRowHeight());
      //      button.setPreferredSize(new Dimension(20, 20));
            button.setMaximumSize(new Dimension(60, tableDVD.getRowHeight()));

            panneauSupprimer.add(button);
        }
        
        
        panneauPrincipal.add(tableDVD, BorderLayout.CENTER);
        panneauPrincipal.add(panneauSupprimer, BorderLayout.EAST);
        fenetre.pack();
    }
}
